<?php

namespace app\service;

use Workerman\Mqtt\Client;

class MqttService
{
    public static ?MqttService $_instance  = null;
    protected string           $queue_name = 'converter_list';

    /**
     * @return MqttService|mixed
     */
    public static function instance(): ?MqttService
    {
        if (!static::$_instance) static::$_instance = new self();
        return static::$_instance;
    }

    /**
     * @param string $name
     * @return $this
     */
    public function setName(string $name = ''): MqttService
    {
        $this->queue_name = $name;
        return $this;
    }

    /**
     * @return string
     */
    public function getName(): string
    {
        return $this->queue_name;
    }

    /**
     * 投递消息
     *
     * @param array $data
     * @return bool
     */
    public function _send(array $data = []): bool
    {
        $queue = $this->queue_name;
        (new Client())->publish($queue, $data);
        return true;
    }
}